package com.lock.subscribe.util;


import org.springframework.stereotype.Component;

import com.sun.jna.Library;
import com.sun.jna.Native;

/**
 * @ Version: 1.0
 * @ Time: 2022/10/25 16:53
 * @ Description:
 */
@Component
public class SoUtil2 {

    /**
     * so库映射的接口
     */
    private interface NativeCpp extends Library {

        /**
         * 映射的方法
         *
         * @param param
         * @return
         */
    	
    	
    	long  Generate_Algorithm_Password_Imp(int psw_type,char[]  mac, long current_time,
				 long start_time, long duration);
    }

    /**
     * 加载so库
     */
    private static class SoLibImpl implements NativeCpp {
        static NativeCpp INSTANCE;

        static {
            try {
                // 也可以采用其他方法加载so库文件,或将so文件放在项目中
                INSTANCE = (NativeCpp) Native.synchronizedLibrary(Native.load("/mnt/pub/crypto.so", NativeCpp.class));
                System.out.println("Load SoLib success");
            } catch (Exception e) {
                // 异常处理
                INSTANCE = null;
                System.out.println("/crypto.so!");
            }
        }

		@Override
		public long Generate_Algorithm_Password_Imp(int psw_type, char[] mac, long current_time, long start_time,
				long duration) {
			return 0;
		}
    }

    /**
     * 对外暴露的工具方法,在此方法中调用so库中的native方法
     *
     * @param param
     * @return
     */
    public static long invoke(int psw_type,char[]  mac, long current_time,
			 long start_time, long duration) {
        if (null != SoLibImpl.INSTANCE) {
            return SoLibImpl.INSTANCE.Generate_Algorithm_Password_Imp(psw_type,mac,current_time,start_time,duration);
        } else {
            throw new RuntimeException("无法执行native方法,请检查so库文件是否存在!");
        }
    }
}

